package paiement;
import javax.swing.JFrame;
import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.awt.Cursor;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;
import java.awt.SystemColor;
import javax.swing.border.EtchedBorder;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.DebugGraphics;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Window.Type;
import java.awt.ComponentOrientation;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.Dialog.ModalExclusionType;





public class Contact_form<ProgressDialog> extends JFrame {
	PaymentProcessor p;
	private String search;
	private ProgressDialog proresDialog;
	private JTextField textNumCarte;
	private JTextField  textDate;
	private JTextField textCcv;
	private JTextField textPrenom;
	private JTextField textNom;
	private JTextField textAdresse;
	private JTextField textVille;
	private JTextField  textEtat;
	private JTextField textCode;
	private JTextField textPays;
	private JTextField textDevise;
	private JTextField textMontant;
	
	//le constructeur
   
	public Contact_form() {
		setResizable(false);
		setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		setForeground(new Color(0, 0, 0));
		setBackground(new Color(255, 255, 255));
		setAlwaysOnTop(true);
		getContentPane().setBackground(new Color(0, 0, 205));
		setFocusableWindowState(false);
		setSize(new Dimension(460, 488));
		setMaximumSize(new Dimension(2147483640, 2147483640));
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Christelle\\Desktop\\icones-e-commerce-22\\finance_icons\\PNG\\png256\\credit_card.png"));
		
		
		setPreferredSize(new Dimension(2, 2));
		//titre du formulaire
		setTitle("Paiement par carte bancaire");
		//definition du layout principal
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		//creation d'un panel en position nord(en haut) du formulaire
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setForeground(Color.BLACK);
		panel.setBackground(new Color(153, 204, 102));
		panel.setToolTipText("");
		panel.setPreferredSize(new Dimension(10, 50));
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(null);
		
		
		JLabel lblNewLabel = new JLabel("Informations personnelles");
		lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Christelle\\Desktop\\icones-e-commerce-33\\icons\\user_suit_black.png"));
		lblNewLabel.setBounds(118, 11, 244, 28);
		lblNewLabel.setFont(new Font("Book Antiqua", Font.PLAIN, 18));
		lblNewLabel.setForeground(Color.WHITE);
		panel.add(lblNewLabel);
		
		
		//panel contenant les boutons
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_2.setPreferredSize(new Dimension(10, 70));
		panel_2.setBackground(new Color(245, 245, 245));
		getContentPane().add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(null);
		
		//le bouton BUY
		JButton btnBuy = new JButton("Buy");
		btnBuy.setBounds(174, 11, 81, 39);
		panel_2.add(btnBuy);
		btnBuy.setForeground(new Color(0, 139, 139));
		btnBuy.setIcon(new ImageIcon("C:\\Users\\Christelle\\Desktop\\icones-e-commerce-22\\finance_icons\\PNG\\png24\\coins.png"));
		btnBuy.addActionListener(new ActionListener() {
		

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				    //searchResult();
					PaymentProcessor p = new PaymentProcessor();
					
					p.setX_amount(textMontant.getText());
					p.setX_card_num(textNumCarte.getText());
					p.setX_address(textAdresse.getText());
					p.setX_exp_date(textDate.getText());
					p.setX_first_name(textPrenom.getText());
					p.setX_last_name(textNom.getText());
					p.setX_zip(textCode.getText());

					
					try {
						String r=p.doPayment();
						if (searchResult(r)){
							JOptionPane.showMessageDialog(null, "Traitement correctement effectue");
						}else{
							String [] responses = r.split("\\|");
							if (responses.length>=3){
							JOptionPane.showMessageDialog(null, "Traitement pas correctement effectue \n"+responses[3]);
							}else{

								JOptionPane.showMessageDialog(null, "Traitement pas correctement effectue");
							}
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(null, e.getMessage());
					}	
			}	
			
		}
		);
		btnBuy.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnBuy.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		btnBuy.setIconTextGap(8);
		btnBuy.setFont(new Font("Book Antiqua", Font.BOLD, 13));
		btnBuy.setBackground(SystemColor.control);
		
		
		
		
		//le bouton annuler
		JButton btnAnnuler = new JButton("Cancel");
		btnAnnuler.setBounds(265, 11, 75, 39);
		panel_2.add(btnAnnuler);
		btnAnnuler.setIcon(new ImageIcon("C:\\Users\\Christelle\\Desktop\\icones-e-commerce-33\\icons\\application_osx_delete.png"));
		btnAnnuler.setForeground(new Color(0, 139, 139));
		btnAnnuler.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				 textNumCarte.setText("");
				 textDate.setText("");
				 textCcv.setText("");
				 textPrenom.setText("");
				 textNom.setText("");
				 textAdresse.setText("");
				 textVille.setText("");
				 textEtat.setText("");
				 textCode.setText("");
				 textPays.setText("");
				 textDevise.setText("");
				 textMontant.setText("");
			}
		});
		btnAnnuler.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnAnnuler.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		btnAnnuler.setBackground(new Color(230, 230, 250));
		btnAnnuler.setFont(new Font("Book Antiqua", Font.BOLD, 13));
		
		//le bouton quitter
		JButton btnQuitter = new JButton("Exit");
		btnQuitter.setBounds(350, 11, 68, 39);
		panel_2.add(btnQuitter);
		btnQuitter.setIcon(new ImageIcon("C:\\Users\\Christelle\\Desktop\\icones-e-commerce-33\\icons\\bullet_cross.png"));
		btnQuitter.setForeground(new Color(0, 139, 139));
		btnQuitter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				System.exit(0);
			}
		});
		btnQuitter.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		btnQuitter.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnQuitter.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		btnQuitter.setBackground(new Color(230, 230, 250));
		btnQuitter.setFont(new Font("Book Antiqua", Font.BOLD, 13));
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setForeground(Color.BLACK);
		panel_1.setBackground(new Color(245, 245, 245));
		getContentPane().add(panel_1, BorderLayout.CENTER);
		
		
		
		JLabel label = new JLabel("Credit Card Number");
		label.setForeground(Color.BLACK);
		label.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label.setBounds(10, 21, 142, 14);
		panel_1.add(label);
		
		JLabel label_1 = new JLabel("Exp");
		label_1.setForeground(Color.BLACK);
		label_1.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_1.setBounds(172, 21, 46, 14);
		panel_1.add(label_1);
		
		JLabel label_2 = new JLabel("CCV");
		label_2.setForeground(Color.BLACK);
		label_2.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_2.setBounds(322, 21, 46, 14);
		panel_1.add(label_2);
		
		textNumCarte = new JTextField();
		textNumCarte.setText("6011000000000012");
		textNumCarte.setForeground(Color.BLACK);
		textNumCarte.setColumns(10);
		textNumCarte.setCaretColor(Color.BLACK);
		textNumCarte.setBackground(Color.WHITE);
		textNumCarte.setBounds(10, 46, 142, 33);
		panel_1.add(textNumCarte);
		
		 textDate = new JTextField();
		 textDate.setText("04/17");
		 textDate.setColumns(10);
		 textDate.setBounds(162, 46, 132, 33);
		panel_1.add( textDate);
		
		textCcv = new JTextField();
		textCcv.setText("782");
		textCcv.setColumns(10);
		textCcv.setBounds(302, 46, 133, 33);
		panel_1.add(textCcv);
		
		JLabel label_3 = new JLabel("First Name");
		label_3.setForeground(Color.BLACK);
		label_3.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_3.setBounds(10, 102, 78, 14);
		panel_1.add(label_3);
		
		JLabel label_4 = new JLabel("Last Name");
		label_4.setForeground(Color.BLACK);
		label_4.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_4.setBounds(172, 102, 70, 14);
		panel_1.add(label_4);
		
		textPrenom = new JTextField();
		textPrenom.setText("John");
		textPrenom.setColumns(10);
		textPrenom.setBounds(10, 127, 142, 33);
		panel_1.add(textPrenom);
		
		textNom = new JTextField();
		textNom.setText("Doe");
		textNom.setColumns(10);
		textNom.setBounds(162, 127, 132, 33);
		panel_1.add(textNom);
		
		JLabel label_5 = new JLabel("Adresse");
		label_5.setForeground(Color.BLACK);
		label_5.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_5.setBounds(10, 176, 70, 14);
		panel_1.add(label_5);
		
		JLabel label_6 = new JLabel("City");
		label_6.setForeground(Color.BLACK);
		label_6.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_6.setBounds(172, 174, 46, 19);
		panel_1.add(label_6);
		
		textAdresse = new JTextField();
		textAdresse.setText("123 Main Street");
		textAdresse.setColumns(10);
		textAdresse.setBounds(10, 195, 142, 33);
		panel_1.add(textAdresse);
		
		textVille = new JTextField();
		textVille.setText("Boston");
		textVille.setColumns(10);
		textVille.setBounds(162, 195, 132, 32);
		panel_1.add(textVille);
		
		JLabel label_7 = new JLabel("State");
		label_7.setForeground(Color.BLACK);
		label_7.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_7.setBounds(10, 246, 46, 14);
		panel_1.add(label_7);
		
		JLabel label_8 = new JLabel("Zip Code");
		label_8.setForeground(Color.BLACK);
		label_8.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_8.setBounds(172, 246, 70, 14);
		panel_1.add(label_8);
		
		JLabel label_9 = new JLabel("Country");
		label_9.setForeground(Color.BLACK);
		label_9.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_9.setBounds(322, 246, 61, 14);
		panel_1.add(label_9);
		
		 textEtat = new JTextField();
		 textEtat.setText("MA");
		 textEtat.setColumns(10);
		 textEtat.setBounds(10, 271, 142, 33);
		panel_1.add( textEtat);
		
		textCode = new JTextField();
		textCode.setText("02142");
		textCode.setColumns(10);
		textCode.setBounds(162, 271, 132, 33);
		panel_1.add(textCode);
		
		textPays= new JTextField();
		textPays.setText("US");
		textPays.setColumns(10);
		textPays.setBounds(302, 271, 133, 33);
		panel_1.add(textPays);
		
		textDevise = new JTextField();
		textDevise.setText("$ USA");
		textDevise.setColumns(10);
		textDevise.setBounds(302, 127, 133, 33);
		panel_1.add(textDevise);
		
		textMontant = new JTextField();
		textMontant.setText("5000");
		textMontant.setColumns(10);
		textMontant.setBounds(302, 195, 133, 33);
		panel_1.add(textMontant);
		
		JLabel label_10 = new JLabel("Currency");
		label_10.setForeground(Color.BLACK);
		label_10.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_10.setBounds(322, 103, 85, 14);
		panel_1.add(label_10);
		
		JLabel label_11 = new JLabel("Amount");
		label_11.setForeground(Color.BLACK);
		label_11.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		label_11.setBounds(322, 177, 85, 14);
		panel_1.add(label_11);
		
	}
	


	
	//voici une ligne qui explique cherche l'etat du paiemt a partir de la reponse qu'on recoit
	
	//methode qui me casse la tete
	public	boolean searchResult(String motif,String chaine) {
		
		 this.search = motif;
		// String result = "le paiement a reussi";
		// result="This transaction has been approved.";
		 
		 Pattern pattern = Pattern.compile(search);
		 Matcher  matcher = pattern.matcher(chaine);
		 
		 if(matcher.find()){
		  System.out.print("trouver ici");
		  return true;
		 }else{
		  System.out.print("pas trouve ici");
		  return false;
		 }
		// boolean etatDuPaiement = this.SearchResult("reussi");
		}
	public	boolean searchResult(String chaine) {
		String motif="This transaction has been approved.";
		 return this.searchResult(motif,chaine);
	}



	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		Contact_form cf=new Contact_form();
		String msg="le paiement a reussi ";
		boolean r=cf.searchResult(msg);
		cf.setSize(500, 500);
		cf.setLocation(400, 150);
		cf.setVisible(true);
		
		
		
	}

	
	}



